import React from 'react';
import {connect} from 'dva/index';
import {Button, Col, Form, Input, Popconfirm, Row, Table} from 'antd';
import Constants from '../../utils/Constants';
import RightConstants from '../../utils/RightConstants';
import RightBox from '../../components/common/rightBox';
import AirCompanyDetail from '../../components/quotationSupport/airCompany/airCompanyDetail';
// import ImportFile from '../../components/common/importFile';
const R = require('ramda');
const FormItem = Form.Item;

const airCompany = Form.create()(class AirCompany extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.props.dispatch({ type: 'airCompany/getListForPage', payload: { page: Constants.DEFAULT_PAGE } });
    }

    handleOpen = (id, disabled) => {
        this.props.dispatch({
            type: 'airCompany/getAirCompanyById',
            payload: { id }
        })
        this.airCompanyDetail.handleOpen(disabled);
    }
    
    // handleImport = (module) =>{
    //     this.importFile.handleOpen(module);
    // }

    handleDelete = id => {
        this.props.dispatch({
            type: 'airCompany/deleteAirCompanyById',
            payload: {
                id,
                onSuccess: this.handleSearch
            }
        })
    }

    handleSearch = flag => {
        let newPage = this.props.airCompany.page;
        if (flag) newPage.page = 1;
        this.props.dispatch({
            type: 'airCompany/getListForPage',
            payload: {
                ...this.props.form.getFieldsValue(),
                page: newPage
            }
        });
    };

    columns = [
        { title: '航空公司代码', dataIndex: 'aviationCode', key: 'aviationCode' },
        { title: '航空公司中文名称', dataIndex: 'aviationName', key: 'aviationName' },
        { title: '航空公司英文名称', dataIndex: 'aviationNameEn', key: 'aviationNameEn' },
        { title: '创建时间', dataIndex: 'createtime', key: 'createtime' },
        {
            title: '操作',
            render: (record) => {
                let editLink = <RightBox resource={RightConstants.QUOTATIONSUPPORT.QUOTATIONSUPPORT_AIRCOMPANY_EDIT} split={true}><a href="#" onClick={this.handleOpen.bind(this, record.id, false)}>编辑</a></RightBox>
                let deleteLink = <Popconfirm onConfirm={this.handleDelete.bind(this, record.id)} okText="是" cancelText="否"
                    placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除?'}>
                    <a href="#">删除</a>
                </Popconfirm>
                deleteLink = <RightBox resource={RightConstants.QUOTATIONSUPPORT.QUOTATIONSUPPORT_AIRCOMPANY_DELETE} split={true}>{deleteLink}</RightBox>
                let viewLink = <a href="#" onClick={this.handleOpen.bind(this, record.id, true)}>查看</a>
                return <span>{editLink}{deleteLink}{viewLink}</span>
            }
        }
    ];

    render() {
        let formItemLayout = { labelCol: { span: 8 }, wrapperCol: { span: 16 } };
        const { getFieldDecorator } = this.props.form;
        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            current: this.props.airCompany.page.page,
            pageSize: this.props.airCompany.page.size,
            total: this.props.airCompany.page.totalRow,
            showSizeChanger: true,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.airCompany.page;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'airCompany/getListForPage',
                    payload: {
                        ...this.props.form.getFieldsValue(),
                        page: newPage
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.airCompany.page;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'airCompany/getListForPage',
                    payload: {
                        ...this.props.form.getFieldsValue(),
                        page: newPage
                    }
                });
            }
        }

        return (
            <div>
                <Row>
                    <Col span={8}><FormItem {...formItemLayout} label={'航空公司'}>{getFieldDecorator('aviation')(<Input maxLength="20" placeholder="可输入航空公司代码、中英文名称查询" />)}</FormItem></Col>
                    <Col span={11} style={{ marginLeft: 42}}>
                        <Button type="primary" icon="search" style={{ marginRight: 5 }} onClick={this.handleSearch.bind(this, true)}>查询</Button>
                        <RightBox resource={RightConstants.QUOTATIONSUPPORT.QUOTATIONSUPPORT_AIRCOMPANY_ADD}>
                            <Button type="primary" icon="plus" style={{ marginRight: 5 }} onClick={this.handleOpen.bind(this, null, false)}>新增航空公司</Button>
                        </RightBox>
                        {/* <RightBox resource={RightConstants.QUOTATIONSUPPORT.QUOTATIONSUPPORT_AIRCOMPANY_IMPORT}>
                            <Button type="primary" icon="plus" style={{ marginRight: 5 }} onClick={this.handleImport.bind(this, Constants.IMPORT_MODULE[3])} size={'default'}>导入航空公司</Button>
                        </RightBox> */}
                    </Col>
                </Row>

                <Table loading={this.props.loading} rowKey='id' dataSource={this.props.airCompany.list} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} />
                <AirCompanyDetail wrappedComponentRef={(inst) => this.airCompanyDetail = inst} refreshList={this.handleSearch} />
                {/* <ImportFile wrappedComponentRef={(inst) => this.importFile = inst} refreshList={this.handleSearch} /> */}
            </div>
        )
    }
})

function mapStateToProps(state) {
    return {
        loading: state.loading.models.airCompany, airCompany: state.airCompany
    };
}

export default connect(mapStateToProps)(airCompany);